package Access;

import DAO.SimpleAccessorFactory;
import Entities.Album;
import Entities.Composition;
import Entities.ExecutorWithInstrument;
import Entities.Genre;
import Entities.Group;
import Entities.LinkAlbumAndComposition;
import Entities.LinkCompositionAndGenre;
import Entities.LinkExecutorWithInstrumentAndComposition;
import Entities.LinkGroupAndComposition;
import Entities.RatingVoice;
import Entities.URL;
import Entities.User;
import Exceptions.InvalidEntityException;
import ExtEntities.ExtComposition;
import ExtEntities.VeryExtComposition;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;

/**
 *
 * @author 647_2
 */
@Stateless
public class Compositions implements CompositionsRemote {

    public LinkedList<ExtComposition> getAllCompositions() {
        try {
            System.out.println("Запрос всех композиций");
            Composition c = new Composition();
            LinkedList<Composition> compositions = SimpleAccessorFactory.createSimpleAccessor(Composition.class).select(c);

            LinkedList<ExtComposition> extcompositions = new LinkedList<ExtComposition>();

            for (int i = 0; i < compositions.size(); i++){
                extcompositions.add(this.getComposition(compositions.get(i).getIDAsInt()));
            }

            return extcompositions;

        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public LinkedList<ExtComposition> getAllCompositions(int start, int count) {
        try {
            System.out.println("Запрос всех композиций");
            Composition c = new Composition();
            LinkedList<Composition> compositions = SimpleAccessorFactory.createSimpleAccessor(Composition.class).select(c, start, count);

            LinkedList<ExtComposition> extcompositions = new LinkedList<ExtComposition>();

            for (int i = 0; i < compositions.size(); i++){
                extcompositions.add(this.getComposition(compositions.get(i).getIDAsInt()));
            }

            return extcompositions;

        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public LinkedList<ExtComposition> getCompositionsByGroupID(int groupID, int start, int count) {
        try {
            System.out.println("Запрос композиций по ID жанра");
            LinkGroupAndComposition link = new LinkGroupAndComposition(new Group(groupID, null, null), null);
            LinkedList<LinkGroupAndComposition> links = SimpleAccessorFactory.createSimpleAccessor(link).select(link, start, count);

            LinkedList<ExtComposition> extcompositions = new LinkedList<ExtComposition>();
            ExtComposition temp;

            for (int i = 0; i < links.size(); i++){
                temp = this.getComposition(links.get(i).get_composition().getIDAsInt());
                extcompositions.add(temp);
            }

            return extcompositions;

        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public LinkedList<ExtComposition> getCompositionsByAlbumID(int albumID, int start, int count) {
        try {
            System.out.println("Запрос композиций по ID жанра");
            LinkAlbumAndComposition link = new LinkAlbumAndComposition(new Album(albumID, null, null), null);
            LinkedList<LinkAlbumAndComposition> links = SimpleAccessorFactory.createSimpleAccessor(link).select(link, start, count);

            LinkedList<ExtComposition> extcompositions = new LinkedList<ExtComposition>();
            ExtComposition temp;

            for (int i = 0; i < links.size(); i++){
                temp = this.getComposition(links.get(i).get_composition().getIDAsInt());
                extcompositions.add(temp);
            }

            return extcompositions;

        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public LinkedList<ExtComposition> getCompositionsByGenreID(int genreID, int start, int count) {
        try {
            System.out.println("Запрос композиций по ID жанра");
            LinkCompositionAndGenre link = new LinkCompositionAndGenre(new Genre(genreID, null, null), null);
            LinkedList<LinkCompositionAndGenre> links = SimpleAccessorFactory.createSimpleAccessor(link).select(link, start, count);

            LinkedList<ExtComposition> extcompositions = new LinkedList<ExtComposition>();
            ExtComposition temp;

            for (int i = 0; i < links.size(); i++){
                temp = this.getComposition(links.get(i).get_composition().getIDAsInt());
                extcompositions.add(temp);
            }

            return extcompositions;

        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public ExtComposition getComposition(int ID) {
        try {

            Composition c = new Composition(ID, null, null);

            LinkedList<Composition> temp = SimpleAccessorFactory.createSimpleAccessor(c).select(c);

            if (temp.size() > 0){

            c = temp.get(0);

            LinkGroupAndComposition lgac = new LinkGroupAndComposition(null, c);
            LinkedList<LinkGroupAndComposition> lgacs = SimpleAccessorFactory.createSimpleAccessor(lgac).select(lgac);

            LinkedList<Group> groups = new LinkedList<Group>();

            for (int i = 0; i < lgacs.size(); i++){
                groups.add(lgacs.get(i).get_group());
            }

            LinkAlbumAndComposition laac = new LinkAlbumAndComposition(null, c);
            LinkedList<LinkAlbumAndComposition> laacs = SimpleAccessorFactory.createSimpleAccessor(laac).select(laac);

            LinkedList<Album> albums = new LinkedList<Album>();

            for (int i = 0; i < laacs.size(); i++){
                albums.add(laacs.get(i).get_album());
            }

            LinkCompositionAndGenre lcag = new LinkCompositionAndGenre(null, c);
            LinkedList<LinkCompositionAndGenre> lcags = SimpleAccessorFactory.createSimpleAccessor(lcag).select(lcag);

            LinkedList<Genre> genres = new LinkedList<Genre>();

            for (int i = 0; i < lcags.size(); i++){
                genres.add(lcags.get(i).get_genre());
            }

            ExtComposition comp = new ExtComposition(ID, c.get_name(), c.get_duration(), groups, albums, genres);

            return comp;

            } else {

                return null;
            }

        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public VeryExtComposition getFullComposition(int ID) {
        try {
            
            Composition c = new Composition(ID, null, null);

            LinkedList<Composition> temp = SimpleAccessorFactory.createSimpleAccessor(c).select(c);

            if (temp.size() > 0){

            c = temp.get(0);

            LinkGroupAndComposition lgac = new LinkGroupAndComposition(null, c);
            LinkedList<LinkGroupAndComposition> lgacs = SimpleAccessorFactory.createSimpleAccessor(lgac).select(lgac);

            LinkedList<Group> groups = new LinkedList<Group>();

            for (int i = 0; i < lgacs.size(); i++){
                groups.add(lgacs.get(i).get_group());
            }

            LinkAlbumAndComposition laac = new LinkAlbumAndComposition(null, c);
            LinkedList<LinkAlbumAndComposition> laacs = SimpleAccessorFactory.createSimpleAccessor(laac).select(laac);

            LinkedList<Album> albums = new LinkedList<Album>();

            for (int i = 0; i < laacs.size(); i++){
                albums.add(laacs.get(i).get_album());
            }

            LinkCompositionAndGenre lcag = new LinkCompositionAndGenre(null, c);
            LinkedList<LinkCompositionAndGenre> lcags = SimpleAccessorFactory.createSimpleAccessor(lcag).select(lcag);

            LinkedList<Genre> genres = new LinkedList<Genre>();

            for (int i = 0; i < lcags.size(); i++){
                genres.add(lcags.get(i).get_genre());
            }

            LinkExecutorWithInstrumentAndComposition lewiac = new LinkExecutorWithInstrumentAndComposition(null, c);
            LinkedList<LinkExecutorWithInstrumentAndComposition> lewiacs = SimpleAccessorFactory.createSimpleAccessor(lewiac).select(lewiac);

            LinkedList<ExecutorWithInstrument> ewis = new LinkedList<ExecutorWithInstrument>();

            for (int i = 0; i < lewiacs.size(); i++){
                ewis.add(lewiacs.get(i).get_executorwithinstrument());
            }

            URL url = new URL(null, c);
            LinkedList<URL> urls = SimpleAccessorFactory.createSimpleAccessor(url).select(url);

            RatingVoice rv = new RatingVoice(null, c, null, null);
            LinkedList<RatingVoice> rvs = SimpleAccessorFactory.createSimpleAccessor(rv).select(rv);

            double rating = 0.0;
            double summa = 0.0;

            for (int i = 0; i < rvs.size(); i++){
                summa += rvs.get(i).get_numberOfRating();
            }

            if (rvs.size() != 0){
                rating = summa / (double)rvs.size();
            } else {
                rating = -1.0;
            }
            
            VeryExtComposition comp = new VeryExtComposition(ID, c.get_name(), c.get_duration(), groups, albums, genres, ewis, urls, rvs, rating);

            return comp;

            } else {

                return null;
            }

        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }


    public boolean AddComposition(String name, Double duration){
        try {
            Composition comp = new Composition(name, duration);
            return SimpleAccessorFactory.createSimpleAccessor(comp).insert(comp);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean UpdateComposition(int ID, String name, Double duration){
        try {
            Composition comp = new Composition(ID, null, null);
            Composition newcomp = new Composition(name, duration);
            return SimpleAccessorFactory.createSimpleAccessor(comp).update(comp, newcomp);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }


    public boolean AddGroupToComposition(int ID, int gID){
        try {
            Group gr = new Group(gID, null, null);
            Composition comp = new Composition(ID, null, null);
            LinkGroupAndComposition lgac = new LinkGroupAndComposition(gr, comp);
            return SimpleAccessorFactory.createSimpleAccessor(lgac).insert(lgac);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean DeleteGroupOfComposition(int ID, int gID){
        try {
            Group gr = new Group(gID, null, null);
            Composition comp = new Composition(ID, null, null);
            LinkGroupAndComposition lgac = new LinkGroupAndComposition(gr, comp);
            return SimpleAccessorFactory.createSimpleAccessor(lgac).delete(lgac);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

        public boolean AddGenreToComposition(int ID, int gID){
        try {
            Genre ge = new Genre(gID, null, null);
            Composition comp = new Composition(ID, null, null);
            LinkCompositionAndGenre lcag = new LinkCompositionAndGenre(ge, comp);
            return SimpleAccessorFactory.createSimpleAccessor(lcag).insert(lcag);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean DeleteGenreOfComposition(int ID, int gID){
        try {
            Genre ge = new Genre(gID, null, null);
            Composition comp = new Composition(ID, null, null);
            LinkCompositionAndGenre lcag = new LinkCompositionAndGenre(ge, comp);
            return SimpleAccessorFactory.createSimpleAccessor(lcag).delete(lcag);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean AddExecutorWithInstrumentToComposition(int ID, int ewiID){
        try {
            ExecutorWithInstrument ewi = new ExecutorWithInstrument(ewiID, null, null);
            Composition comp = new Composition(ID, null, null);
            LinkExecutorWithInstrumentAndComposition lewiac = new LinkExecutorWithInstrumentAndComposition(ewi, comp);
            return SimpleAccessorFactory.createSimpleAccessor(lewiac).insert(lewiac);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean DeleteExecutorWithInstrumentOfComposition(int ID, int ewiID){
        try {
            ExecutorWithInstrument ewi = new ExecutorWithInstrument(ewiID, null, null);
            Composition comp = new Composition(ID, null, null);
            LinkExecutorWithInstrumentAndComposition lewiac = new LinkExecutorWithInstrumentAndComposition(ewi, comp);
            return SimpleAccessorFactory.createSimpleAccessor(lewiac).delete(lewiac);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

        public boolean AddURLToComposition(int ID, String url){
        try {
            Composition comp = new Composition(ID, null, null);
            URL newurl = new URL(url, comp);

            return SimpleAccessorFactory.createSimpleAccessor(newurl).insert(newurl);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean DeleteURLOfComposition(int urlID){
        try {
            URL url = new URL(urlID, null, null);
            return SimpleAccessorFactory.createSimpleAccessor(url).delete(url);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean DeleteComposition(int ID){
        try {
            Composition comp = new Composition(ID, null, null);
            return SimpleAccessorFactory.createSimpleAccessor(comp).delete(comp);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean AddRatingToComposition(int ID, int uID, double rating, String comment) {
        try {
            RatingVoice rv = new RatingVoice(rating, new Composition(ID, null, null), new User(uID, null, null, null), comment);
            return SimpleAccessorFactory.createSimpleAccessor(rv).insert(rv);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }
}
